package com.salim.leetcode.$155;

/**
 * Created with IntelliJ IDEA.
 * User: clevo
 * Date: 15-7-9
 * Time: 上午12:29
 * To change this template use File | Settings | File Templates.
 */
public class MinStackArray {

    Integer[] stack = new Integer[100];
    int elementsNum = 0;

    public void push(int x) {
        //如果超出了数组长度 新建一个数组
        if(stack.length <= elementsNum){
            Object[] stackOld = stack;
            stack = new Integer[stackOld.length + (int)(stackOld.length * 0.75)];
            System.arraycopy(stackOld,0,stack,0,stackOld.length);
        }
        stack[elementsNum] = x;
        elementsNum++;
    }

    public void pop() {
        stack[elementsNum-1] = null;
        elementsNum--;
    }

    public int top() {
        return stack[elementsNum-1];
    }

    public int getMin() {
        int max = stack[0];
        for(int i=0;i<stack.length;i++){
            if(null != stack[i] ){
                if(stack[i] < max){
                    max = stack[i];
                }
            }else{
                break;
            }

        }
        return max;
    }

    public static void main(String[] args){
        MinStackArray minStackArray = new MinStackArray();
        minStackArray.push(1);
        minStackArray.push(13);
        minStackArray.push(5);
        System.out.println(minStackArray.stack.toString());
        minStackArray.push(17);
        minStackArray.push(19);
        minStackArray.push(-1);
        System.out.println(minStackArray.getMin());
        minStackArray.pop();
        System.out.println(minStackArray.top());
    }
}
